package edu.ynu.e_platform.controllers;

import edu.ynu.e_platform.dto.ShopItemDTO;
import org.modelmapper.ModelMapper;
import org.springframework.data.domain.Page;
import edu.ynu.e_platform.common.controller.LogicController;
import edu.ynu.e_platform.dao.ShopItemDao;
import edu.ynu.e_platform.entities.Item;
import edu.ynu.e_platform.entities.ShopItem;
import edu.ynu.e_platform.service.ShopItemService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author manwen Yang
 */
@Slf4j
@Tag(name = "商店商品 实体的控制器")
@RestController
@RequestMapping("/shopItem")
public class ShopItemController
        extends LogicController<ShopItemService, ShopItemDao, ShopItem, Long> {

//    ModelMapper modelMapper = new ModelMapper();
    @Resource
    ModelMapper modelMapper;

    public ShopItemController(@Autowired ShopItemService ls) {
        super(ls);
    }

    @GetMapping("/queryAll")
    @Operation(summary = "查询所有上架商品")
    public List<ShopItemDTO> queryAll(){
        List<ShopItem> results = getService().queryAll();
        return results.stream().map(
                shopItem -> modelMapper.map(shopItem, ShopItemDTO.class)
        ).collect(Collectors.toList());
    }

    @GetMapping("/queryAllPage")
    @Operation(summary = "分页 查询所有上架商品")
    public List<ShopItemDTO> pageQueryAll(Integer pageStart,Integer pageSize){

        Page<ShopItem> results = getService().PageQueryByItemStatus(pageStart,pageSize,1);

        return results.stream().map(
                shopItem -> modelMapper.map(shopItem, ShopItemDTO.class)
        ).collect(Collectors.toList());
    }
}
